﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.Messaging;
using System.Web.Security;
using Reviewer.Business;
using DotNetOpenAuth.OpenId;

public partial class Auth_OpenId_OpenId : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uiTbOpenIdLogin.Focus();

        OpenIdRelyingParty openid = new OpenIdRelyingParty();

        var response = openid.GetResponse();
        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    // This is where you would look for any OpenID extension responses included  
                    // in the authentication assertion.  
                    var claimsResponse = response.GetExtension<ClaimsResponse>();

                    if (response.ClaimedIdentifier != null)
                    {
                        string userName = ServiceFactory.MembersAttributeService.GetUserNameFromAttribute("OpenId", response.ClaimedIdentifier);
                        if (!String.IsNullOrEmpty(userName))
                        {
                            // Use FormsAuthentication to tell ASP.NET that the user is now logged in,  
                            // with the OpenID Claimed Identifier as their username.  
                            FormsAuthentication.RedirectFromLoginPage(userName, false);
                        }
                        else
                        {
                            this.uiLbLoginFailed.Visible = true;
                        }
                    }
                    else
                        this.uiLbLoginFailed.Visible = true;
                    break;
                case AuthenticationStatus.Canceled:
                    this.uiLbLoginCanceled.Visible = true;
                    break;
                case AuthenticationStatus.Failed:
                    this.uiLbLoginFailed.Visible = true;
                    break;
            }
        }
    }

    protected void uiCvOpenId_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // This catches common typos that result in an invalid OpenID Identifier.  
        args.IsValid = Identifier.IsValid(args.Value);
    }

    protected void uiBnLogin_Click(object sender, EventArgs e)
    {
        if (!this.Page.IsValid)
        {
            return; // don't login if custom validation failed.  
        }

        try
        {
            using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
            {
                IAuthenticationRequest request = openid.CreateRequest(this.uiTbOpenIdLogin.Text);

                /*
                // This is where you would add any OpenID extensions you wanted  
                // to include in the authentication request.  
                request.AddExtension(new ClaimsRequest
                {
                    Country = DemandLevel.Request,
                    Email = DemandLevel.Request,
                    Gender = DemandLevel.Require,
                    PostalCode = DemandLevel.,
                    TimeZone = DemandLevel.Require,
                });
                 * */

                // Send your visitor to their Provider for authentication.  
                request.RedirectToProvider();
            }
        }
        catch (ProtocolException ex)
        {
            // The user probably entered an Identifier that  
            // was not a valid OpenID endpoint.  
            this.uiCvOpenId.Text = ex.Message;
            this.uiCvOpenId.IsValid = false;
        }
    }
}
